var createError = require('http-errors');
var express = require('express');
var path = require('path');
var https = require('https')
var http = require('http')
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var proxy = require('express-http-proxy');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var websitesRouter = require('./routes/websites');
var userRouter = require('./routes/user');
var tourRouter = require('./routes/tour');

const config = require('./config')
var app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());//数据JSON类型
app.use(bodyParser.urlencoded({ extended: false }));//解析post请求数据


// view engine setup
console.log(path.join(__dirname, 'views'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, config.publicPath)));


// app.all('*',function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', '*');
//   res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
//   res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
 
//   if (req.method == 'OPTIONS') {
//     res.send(200); /让options请求快速返回/
//   }
//   else {
//     next();
//   }
// });



app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use('/websites', websitesRouter);
app.use('/user', userRouter);
app.use('/tour', tourRouter);

// app.use('/api/*', proxy('https://cd.city.qq.com/'))
app.use('/api/*', function (req, res) {
  var option = {
    hostname: 'cd.city.qq.com',
    path: '/interface/skanda/GetSubNew?cid=30101&page=1&size=17',
    method:'GET',
    headers: {
      'Accept': '*/*',
      'Accept-Encoding': 'utf-8',  //这里设置返回的编码方式 设置其他的会是乱码
      'Accept-Language': 'zh-CN,zh;q=0.8',
      'Connection': 'keep-alive',
      'Cookie': '',
      'Host': 'https://cd.city.qq.com/',
      'Referer': 'https://cd.qq.com/travel/'
    }
  };
  // var option = {
  //   hostname: 'www.xiongmaoyouxuan.com',
  //   path: '/api/tab/1?start=0',
  //   method:'GET',
  //   headers: {
  //     // 'Accept': '*/*',
  //     // 'Accept-Encoding': 'utf-8',  //这里设置返回的编码方式 设置其他的会是乱码
  //     // 'Accept-Language': 'zh-CN,zh;q=0.8',
  //     // 'Connection': 'keep-alive',
  //     // 'Cookie': '',
  //     // 'Host': 'https://cd.city.qq.com/',
  //     // 'Referer': 'https://cd.qq.com/travel/'
  //   }
  // };
  const reqt = http.request(option, function (res2) {
    let buf = [];
    res2.on('data', d => {
      buf.push(d);
    })
    res2.on('end', d => {
      buf = Buffer.concat(buf);
      res.send (buf.toString())
    })
  });
  reqt.on('error', (e) => {
    console.error(e);
  });
  reqt.end();
})


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};
  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
app.listen('9090')
module.exports = app;
